home *** CD-ROM | disk | FTP | other *** search
/ AGA Toolkit '97 / The AGA Toolkit '97.iso / miscellaneous / science / maths / calc / source / value.h < prev    next >
Encoding:
C/C++ Source or Header  |  1996-09-07  |  12.6 KB  |  347 lines

  1. /*
  2.  * Copyright (c) 1994 David I. Bell
  3.  * Permission is granted to use, distribute, or modify this source,
  4.  * provided that this copyright notice remains intact.
  5.  *
  6.  * Definitions of general values and related routines used by the calculator.
  7.  */
  8.  
  9. #ifndef    VALUE_H
  10. #define    VALUE_H
  11.  
  12. #include "cmath.h"
  13.  
  14.  
  15. #define MAXDIM        4    /* maximum number of dimensions in matrices */
  16. #define USUAL_ELEMENTS    4    /* usual number of elements for objects */
  17.  
  18.  
  19. /*
  20.  * Flags to modify results from the printvalue routine.
  21.  * These flags are OR'd together.
  22.  */
  23. #define    PRINT_NORMAL    0x00    /* print in normal manner */
  24. #define    PRINT_SHORT    0x01    /* print in short format (no elements) */
  25. #define    PRINT_UNAMBIG    0x02    /* print in non-ambiguous manner */
  26.  
  27.  
  28. /*
  29.  * Definition of values of various types.
  30.  */
  31. typedef struct value VALUE;
  32. typedef struct object OBJECT;
  33. typedef struct matrix MATRIX;
  34. typedef struct list LIST;
  35. typedef    struct assoc ASSOC;
  36. typedef    long FILEID;
  37.  
  38.  
  39. struct value {
  40.     short v_type;            /* type of value */
  41.     short v_subtype;        /* other data related to some types */
  42.     union {
  43.         long vv_int;        /* small integer value */
  44.         FILEID vv_file;        /* id of opened file */
  45.         NUMBER *vv_num;        /* arbitrary sized numeric value */
  46.         COMPLEX *vv_com;    /* complex number */
  47.         VALUE *vv_addr;        /* address of variable value */
  48.         MATRIX *vv_mat;        /* address of matrix */
  49.         LIST *vv_list;        /* address of list */
  50.         ASSOC *vv_assoc;    /* address of association */
  51.         OBJECT *vv_obj;        /* address of object */
  52.         char *vv_str;        /* string value */
  53.     } v_union;
  54. };
  55.  
  56.  
  57. /*
  58.  * For ease in referencing
  59.  */
  60. #define v_int    v_union.vv_int
  61. #define    v_file    v_union.vv_file
  62. #define v_num    v_union.vv_num
  63. #define v_com    v_union.vv_com
  64. #define v_addr    v_union.vv_addr
  65. #define v_str    v_union.vv_str
  66. #define v_mat    v_union.vv_mat
  67. #define    v_list    v_union.vv_list
  68. #define    v_assoc    v_union.vv_assoc
  69. #define v_obj    v_union.vv_obj
  70. #define    v_valid    v_union.vv_int
  71.  
  72.  
  73. /*
  74.  * Value types.
  75.  */
  76. #define V_NULL    0    /* null value */
  77. #define V_INT    1    /* normal integer */
  78. #define V_NUM    2    /* number */
  79. #define V_COM    3    /* complex number */
  80. #define V_ADDR    4    /* address of variable value */
  81. #define V_STR    5    /* address of string */
  82. #define V_MAT    6    /* address of matrix structure */
  83. #define    V_LIST    7    /* address of list structure */
  84. #define    V_ASSOC    8    /* address of association structure */
  85. #define V_OBJ    9    /* address of object structure */
  86. #define    V_FILE    10    /* opened file id */
  87. #define V_MAX    10    /* highest legal value */
  88.  
  89. #define V_NOSUBTYPE    0    /* subtype has no meaning */
  90. #define V_STRLITERAL    1    /* string subtype for literal str */
  91. #define V_STRALLOC    2    /* string subtype for allocated str */
  92.  
  93. #define TWOVAL(a,b) ((a) * (V_MAX+1) + (b))    /* for switch of two values */
  94.  
  95. #define    NULL_VALUE    ((VALUE *) 0)
  96.  
  97.  
  98. extern void freevalue MATH_PROTO((VALUE *vp));
  99. extern void copyvalue MATH_PROTO((VALUE *vp, VALUE *vres));
  100. extern void negvalue MATH_PROTO((VALUE *vp, VALUE *vres));
  101. extern void addvalue MATH_PROTO((VALUE *v1, VALUE *v2, VALUE *vres));
  102. extern void subvalue MATH_PROTO((VALUE *v1, VALUE *v2, VALUE *vres));
  103. extern void mulvalue MATH_PROTO((VALUE *v1, VALUE *v2, VALUE *vres));
  104. extern void squarevalue MATH_PROTO((VALUE *vp, VALUE *vres));
  105. extern void invertvalue MATH_PROTO((VALUE *vp, VALUE *vres));
  106. extern void roundvalue MATH_PROTO((VALUE *v1, VALUE *v2, VALUE *vres));
  107. extern void broundvalue MATH_PROTO((VALUE *v1, VALUE *v2, VALUE *vres));
  108. extern void intvalue MATH_PROTO((VALUE *vp, VALUE *vres));
  109. extern void fracvalue MATH_PROTO((VALUE *vp, VALUE *vres));
  110. extern void incvalue MATH_PROTO((VALUE *vp, VALUE *vres));
  111. extern void decvalue MATH_PROTO((VALUE *vp, VALUE *vres));
  112. extern void conjvalue MATH_PROTO((VALUE *vp, VALUE *vres));
  113. extern void sqrtvalue MATH_PROTO((VALUE *v1, VALUE *v2, VALUE *vres));
  114. extern void rootvalue MATH_PROTO((VALUE *v1, VALUE *v2, VALUE *v3,
  115.     VALUE *vres));
  116. extern void absvalue MATH_PROTO((VALUE *v1, VALUE *v2, VALUE *vres));
  117. extern void normvalue MATH_PROTO((VALUE *vp, VALUE *vres));
  118. extern void shiftvalue MATH_PROTO((VALUE *v1, VALUE *v2, BOOL rightshift,
  119.     VALUE *vres));
  120. extern void scalevalue MATH_PROTO((VALUE *v1, VALUE *v2, VALUE *vres));
  121. extern void powivalue MATH_PROTO((VALUE *v1, VALUE *v2, VALUE *vres));
  122. extern void powervalue MATH_PROTO((VALUE *v1, VALUE *v2, VALUE *v3,
  123.     VALUE *vres));
  124. extern void divvalue MATH_PROTO((VALUE *v1, VALUE *v2, VALUE *vres));
  125. extern void quovalue MATH_PROTO((VALUE *v1, VALUE *v2, VALUE *vres));
  126. extern void modvalue MATH_PROTO((VALUE *v1, VALUE *v2, VALUE *vres));
  127. extern BOOL testvalue MATH_PROTO((VALUE *vp));
  128. extern BOOL comparevalue MATH_PROTO((VALUE *v1, VALUE *v2));
  129. extern FLAG relvalue MATH_PROTO((VALUE *v1, VALUE *v2));
  130. extern HASH hashvalue MATH_PROTO((VALUE *vp));
  131. extern void printvalue MATH_PROTO((VALUE *vp, int flags));
  132.  
  133.  
  134.  
  135. /*
  136.  * Structure of a matrix.
  137.  */
  138. struct matrix {
  139.     long m_dim;        /* dimension of matrix */
  140.     long m_size;        /* total number of elements */
  141.     long m_min[MAXDIM];    /* minimum bound for indices */
  142.     long m_max[MAXDIM];    /* maximum bound for indices */
  143.     VALUE m_table[1];    /* actually varying length table */
  144. };
  145.  
  146. #define matsize(n) (sizeof(MATRIX) - sizeof(VALUE) + ((n) * sizeof(VALUE)))
  147.  
  148.  
  149. extern MATRIX *matadd MATH_PROTO((MATRIX *m1, MATRIX *m2));
  150. extern MATRIX *matsub MATH_PROTO((MATRIX *m1, MATRIX *m2));
  151. extern MATRIX *matmul MATH_PROTO((MATRIX *m1, MATRIX *m2));
  152. extern MATRIX *matneg MATH_PROTO((MATRIX *m));
  153. extern MATRIX *matalloc MATH_PROTO((long size));
  154. extern MATRIX *matcopy MATH_PROTO((MATRIX *m));
  155. extern MATRIX *matsquare MATH_PROTO((MATRIX *m));
  156. extern MATRIX *matinv MATH_PROTO((MATRIX *m));
  157. extern MATRIX *matscale MATH_PROTO((MATRIX *m, long n));
  158. extern MATRIX *matshift MATH_PROTO((MATRIX *m, long n));
  159. extern MATRIX *matmulval MATH_PROTO((MATRIX *m, VALUE *vp));
  160. extern MATRIX *matpowi MATH_PROTO((MATRIX *m, NUMBER *q));
  161. extern MATRIX *matconj MATH_PROTO((MATRIX *m));
  162. extern MATRIX *matquoval MATH_PROTO((MATRIX *m, VALUE *vp));
  163. extern MATRIX *matmodval MATH_PROTO((MATRIX *m, VALUE *vp));
  164. extern MATRIX *matint MATH_PROTO((MATRIX *m));
  165. extern MATRIX *matfrac MATH_PROTO((MATRIX *m));
  166. extern MATRIX *matround MATH_PROTO((MATRIX *m, long places));
  167. extern MATRIX *matbround MATH_PROTO((MATRIX *m, long places));
  168. extern MATRIX *mattrans MATH_PROTO((MATRIX *m));
  169. extern MATRIX *matcross MATH_PROTO((MATRIX *m1, MATRIX *m2));
  170. extern BOOL mattest MATH_PROTO((MATRIX *m));
  171. extern BOOL matcmp MATH_PROTO((MATRIX *m1, MATRIX *m2));
  172. extern long matsearch MATH_PROTO((MATRIX *m, VALUE *vp, long index));
  173. extern long matrsearch MATH_PROTO((MATRIX *m, VALUE *vp, long index));
  174. extern HASH mathash MATH_PROTO((MATRIX *m));
  175. extern VALUE matdet MATH_PROTO((MATRIX *m));
  176. extern VALUE matdot MATH_PROTO((MATRIX *m1, MATRIX *m2));
  177. extern void matfill MATH_PROTO((MATRIX *m, VALUE *v1, VALUE *v2));
  178. extern void matfree MATH_PROTO((MATRIX *m));
  179. extern void matprint MATH_PROTO((MATRIX *m, long max_print));
  180. extern VALUE *matindex MATH_PROTO((MATRIX *mp, BOOL create, long dim,
  181.     VALUE *indices));
  182.  
  183.  
  184. #if 0
  185. extern BOOL matisident MATH_PROTO((MATRIX *m));
  186. #endif
  187.  
  188.  
  189.  
  190. /*
  191.  * List definitions.
  192.  * An individual list element.
  193.  */
  194. typedef struct listelem LISTELEM;
  195. struct listelem {
  196.     LISTELEM *e_next;    /* next element in list (or NULL) */
  197.     LISTELEM *e_prev;    /* previous element in list (or NULL) */
  198.     VALUE e_value;        /* value of this element */
  199. };
  200.  
  201.  
  202. /*
  203.  * Structure for a list of elements.
  204.  */
  205. struct list {
  206.     LISTELEM *l_first;    /* first list element (or NULL) */
  207.     LISTELEM *l_last;    /* last list element (or NULL) */
  208.     LISTELEM *l_cache;    /* cached list element (or NULL) */
  209.     long l_cacheindex;    /* index of cached element (or undefined) */
  210.     long l_count;        /* total number of elements in the list */
  211. };
  212.  
  213.  
  214. extern void insertlistfirst MATH_PROTO((LIST *lp, VALUE *vp));
  215. extern void insertlistlast MATH_PROTO((LIST *lp, VALUE *vp));
  216. extern void insertlistmiddle MATH_PROTO((LIST *lp, long index, VALUE *vp));
  217. extern void removelistfirst MATH_PROTO((LIST *lp, VALUE *vp));
  218. extern void removelistlast MATH_PROTO((LIST *lp, VALUE *vp));
  219. extern void removelistmiddle MATH_PROTO((LIST *lp, long index, VALUE *vp));
  220. extern void listfree MATH_PROTO((LIST *lp));
  221. extern void listprint MATH_PROTO((LIST *lp, long max_print));
  222. extern long listsearch MATH_PROTO((LIST *lp, VALUE *vp, long index));
  223. extern long listrsearch MATH_PROTO((LIST *lp, VALUE *vp, long index));
  224. extern HASH listhash MATH_PROTO((LIST *lp));
  225. extern BOOL listcmp MATH_PROTO((LIST *lp1, LIST *lp2));
  226. extern VALUE *listfindex MATH_PROTO((LIST *lp, long index));
  227. extern LIST *listalloc MATH_PROTO((void));
  228. extern LIST *listcopy MATH_PROTO((LIST *lp));
  229.  
  230.  
  231.  
  232. /*
  233.  * Structures for associations.
  234.  * Associations are "indexed" by one or more arbitrary values, and are
  235.  * stored in a hash table with their hash values for quick indexing.
  236.  */
  237. typedef    struct assocelem ASSOCELEM;
  238. struct assocelem {
  239.     ASSOCELEM *e_next;    /* next element in list (or NULL) */
  240.     long e_dim;        /* dimension of indexing for this element */
  241.     HASH e_hash;        /* hash value for this element */
  242.     VALUE e_value;        /* value of association */
  243.     VALUE e_indices[1];    /* index values (variable length) */
  244. };
  245.  
  246.  
  247. struct assoc {
  248.     long a_count;        /* number of elements in the association */
  249.     long a_size;        /* current size of association hash table */
  250.     ASSOCELEM **a_table;    /* current hash table for elements */
  251. };
  252.  
  253.  
  254. extern ASSOC *assocalloc MATH_PROTO((long initsize));
  255. extern ASSOC *assoccopy MATH_PROTO((ASSOC *ap));
  256. extern void assocfree MATH_PROTO((ASSOC *ap));
  257. extern void assocprint MATH_PROTO((ASSOC *ap, long max_print));
  258. extern long assocsearch MATH_PROTO((ASSOC *ap, VALUE *vp, long index));
  259. extern long assocrsearch MATH_PROTO((ASSOC *ap, VALUE *vp, long index));
  260. extern HASH assochash MATH_PROTO((ASSOC *ap));
  261. extern BOOL assoccmp MATH_PROTO((ASSOC *ap1, ASSOC *ap2));
  262. extern VALUE *assocfindex MATH_PROTO((ASSOC *ap, long index));
  263. extern VALUE *associndex MATH_PROTO((ASSOC *ap, BOOL create, long dim,
  264.     VALUE *indices));
  265.  
  266.  
  267. /*
  268.  * Object actions.
  269.  */
  270. #define OBJ_PRINT    0    /* print the value */
  271. #define OBJ_ONE        1    /* create the multiplicative identity */
  272. #define OBJ_TEST    2    /* test a value for "zero" */
  273. #define OBJ_ADD        3    /* add two values */
  274. #define OBJ_SUB        4    /* subtrace one value from another */
  275. #define OBJ_NEG        5    /* negate a value */
  276. #define OBJ_MUL        6    /* multiply two values */
  277. #define OBJ_DIV        7    /* divide one value by another */
  278. #define OBJ_INV        8    /* invert a value */
  279. #define OBJ_ABS        9    /* take absolute value of value */
  280. #define OBJ_NORM    10    /* take the norm of a value */
  281. #define OBJ_CONJ    11    /* take the conjugate of a value */
  282. #define OBJ_POW        12    /* take the power function */
  283. #define OBJ_SGN        13    /* return the sign of a value */
  284. #define OBJ_CMP        14    /* compare two values for equality */
  285. #define OBJ_REL        15    /* compare two values for inequality */
  286. #define OBJ_QUO        16    /* integer quotient of values */
  287. #define OBJ_MOD        17    /* remainder of division of values */
  288. #define OBJ_INT        18    /* integer part of */
  289. #define OBJ_FRAC    19    /* fractional part of */
  290. #define OBJ_INC        20    /* increment by one */
  291. #define OBJ_DEC        21    /* decrement by one */
  292. #define OBJ_SQUARE    22    /* square value */
  293. #define OBJ_SCALE    23    /* scale by power of two */
  294. #define OBJ_SHIFT    24    /* shift left (or right) by number of bits */
  295. #define OBJ_ROUND    25    /* round to specified decimal places */
  296. #define OBJ_BROUND    26    /* round to specified binary places */
  297. #define OBJ_ROOT    27    /* take nth root of value */
  298. #define OBJ_SQRT    28    /* take square root of value */
  299. #define OBJ_MAXFUNC    28    /* highest function */
  300.  
  301.  
  302. /*
  303.  * Definition of an object type.
  304.  * This is actually a varying sized structure.
  305.  */
  306. typedef struct {
  307.     char *name;            /* name of object */
  308.     int count;            /* number of elements defined */
  309.     int actions[OBJ_MAXFUNC+1];    /* function indices for actions */
  310.     int elements[1];        /* element indexes (MUST BE LAST) */
  311. } OBJECTACTIONS;
  312.  
  313. #define objectactionsize(elements) \
  314.     (sizeof(OBJECTACTIONS) + ((elements) - 1) * sizeof(int))
  315.  
  316.  
  317. /*
  318.  * Structure of an object.
  319.  * This is actually a varying sized structure.
  320.  * However, there are always at least USUAL_ELEMENTS values in the object.
  321.  */
  322. struct object {
  323.     OBJECTACTIONS *o_actions;    /* action table for this object */
  324.     VALUE o_table[USUAL_ELEMENTS];    /* object values (MUST BE LAST) */
  325. };
  326.  
  327. #define objectsize(elements) \
  328.     (sizeof(OBJECT) + ((elements) - USUAL_ELEMENTS) * sizeof(VALUE))
  329.  
  330.  
  331. extern OBJECT *objcopy MATH_PROTO((OBJECT *op));
  332. extern OBJECT *objalloc MATH_PROTO((long index));
  333. extern VALUE objcall MATH_PROTO((int action, VALUE *v1, VALUE *v2, VALUE *v3));
  334. extern void objfree MATH_PROTO((OBJECT *op));
  335. extern void objuncache MATH_PROTO((void));
  336. extern int addelement MATH_PROTO((char *name));
  337. extern void defineobject MATH_PROTO((char *name, int indices[], int count));
  338. extern int checkobject MATH_PROTO((char *name));
  339. extern void showobjfuncs MATH_PROTO((void));
  340. extern int findelement MATH_PROTO((char *name));
  341. extern int objoffset MATH_PROTO((OBJECT *op, long index));
  342. extern HASH objhash MATH_PROTO((OBJECT *op));
  343.  
  344. #endif
  345.  
  346. /* END CODE */
  347.